<?php
abstract class Model{
    protected $_class;
    protected $_partialOrder = '_pk DESC';

    public function __construct(){
        $this->_class = get_class($this);
    }

	public function get($id)
	{
		return Db::get($this->_class)->select()->where(array('_pk'=>$id))->getOne();
	}

    public function getCol($id, $field){
        $ary = $this->get($id);
        return ($ary && isset($ary[$field])) ? $ary[$field] : false;
    }

	public function isExists($id)
	{
		return Db::get($this->_class)->count()->where(array('_pk'=>$id))->getCol();
	}

	public function getList($where=null, $s=0, $l=0, $order=null)
	{
		return Db::get($this->_class)->select()->where($where)->order($order?$order:$this->_partialOrder)->limit($s, $l)->hkey('_pk')->getAll();
	}
	public function count($where)
	{
		return Db::get($this->_class)->count()->where($where)->getCol();
	}

	public function add($data)
	{
		return Db::get($this->_class)->insert($data)->exec() ? Db::get($this->_class)->insertId() : false;
	}
	public function update($id, $data)
	{
		return Db::get($this->_class)->update($data)->where(array('_pk'=>$id))->exec();
	}
	public function delete($id)
	{
		return Db::get($this->_class)->delete()->where(array('_pk'=>$id))->exec();
	}
}
?>
